package JFrame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

public class Kill{
    // 加载图片
    ImageIcon bullet_right = new ImageIcon("C:\\Users\\hhp\\Desktop\\Game1\\Image\\bullet_right.gif");
    ImageIcon bullet_left = new ImageIcon("C:\\Users\\hhp\\Desktop\\Game1\\Image\\bullet_left.gif");
    ImageIcon fire = new ImageIcon("C:\\Users\\hhp\\Desktop\\Game1\\Image\\fire.gif");
    BufferedImage zhen;
    public int x;
    public int y;
    private int speed = 9;
    public int type;
    public double life=40;
    public Boolean direction;
    public Boolean release=true;
    public long life_time=0;
    public double i=0.4;

    // 构造函数
    public Kill(int x, int y, Boolean direction, int type) {
        if(type==2){
            this.x = x;
            this.y = y;
            this.type = type;
            this.direction = direction;
            release=false;
            life=7.6;
            try {
                zhen=ImageIO.read(new FileInputStream("C:\\Users\\hhp\\Desktop\\Game1\\Image\\zhen" + 1 + ".png"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }else {
            this.direction = direction;
            this.x = x;
            this.y = y;
            this.type = type;
        }

    }
    // 绘制方法
    public void draw(Graphics g) {
        life-=0.1;
        if (type == 0) {
            if(direction) {
                bullet_right.paintIcon(null, g, x += speed, y);
            }
            else {
                bullet_left.paintIcon(null,g,x-=speed,y);
            }
        } else if(type==1){
            fire.paintIcon(null, g, x, y);
        }else{
            int frameNumber = (int)(i / 0.4) % 20;
                try {
                    zhen =ImageIO.read(new FileInputStream("C:\\Users\\hhp\\Desktop\\Game1\\Image\\zhen" + frameNumber + ".png"));
                    g.drawImage(zhen,x,y,null);
                    i+=0.1;
                    if (i >= 19 * 0.4) i = 0.4;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
        }
    }
    // 获取宽度
    public int getwidth(){
        if(type==0){
            return bullet_left.getIconWidth();
        }
        else if(type==1){
            return bullet_left.getIconWidth();
        }else {
            return zhen.getWidth();
        }
    }
    // 获取高度
    public int getheight(){
        if(type==0){
            return bullet_left.getIconHeight();
        }
        else if(type==1){
            return bullet_left.getIconHeight();
        }else {
            return zhen.getHeight();
        }
    }
    // 设置生命时间
    public void setlife_time(){
        life_time=System.currentTimeMillis();
    }
    // 增加x坐标
    public void addx(){
        if(direction) {
            x += 10;
        }else {
            x-=10;
        }
    }
}
